<?php include _include(ADMIN_PATH.'view/htm/header.inc.htm');?>

<div class="row">
    <div class="col-lg-10 mx-auto">
        <div class="btn-group mb-3" role="group">
            <?php echo admin_tab_active($wechat['setting'], 'menu'); ?>
        </div>
        <div class="card">
            <div class="card-body">
                <form action="<?php echo url("plugin-setting-xn_wechat_public-menu"); ?>" method="post" id="form">
                    <h4 class="card-title"><?php echo lang('wechat_menu');?></h4>
                    <table class="form">
                        <thead class="left">
                        <tr>
                            <th>编号</th>
                            <th>一级菜单</th>
                            <th>二级菜单</th>
                            <th>关键词</th>
                            <th>地址</th>
                            <th>菜单类型</th>
                        </tr>
                        </thead>
                        <tbody id="wechat">
                        <?php foreach ( $wechat_menu as $wechat ) { ?>
                            <tr>
                                <input type="hidden" name="ids[<?php echo $wechat['id'] ?>]"
                                       value="<?php echo $wechat['id'] ?>"/>
                                <input type="hidden" name="pids[<?php echo $wechat['id'] ?>]" value="0"/>
                                <td><i class="icon icon-plus"
                                       fid="<?php echo $wechat['id'] ?>"></i> <?php echo $wechat['id'] ?></td>
                                <td></td>
                                <td style="padding:0 0.1rem"><input type="text" class="form-control"
                                                                    name="names[<?php echo $wechat['id'] ?>]"
                                                                    value="<?php echo $wechat['name'] ?>"/></td>
                                <td style="padding:0 0.1rem"><input type="text" class="form-control"
                                                                    name="keys[<?php echo $wechat['id'] ?>]"
                                                                    value="<?php echo $wechat['key'] ?>"/></td>
                                <td style="padding:0 0.1rem"><input type="text" class="form-control"
                                                                    name="urls[<?php echo $wechat['id'] ?>]"
                                                                    value="<?php echo $wechat['url'] ?>"/></td>
                                <td><?php echo $wechat['type'] ?></td>
                            </tr>
                            <?php foreach ( $wechat['son'] as $_wechat ) { ?>
                                <tr class="forum_son_<?php echo $wechat['id'] ?>" style="display:none;">
                                    <input type="hidden" name="ids[<?php echo $_wechat['id'] ?>]"
                                           value="<?php echo $_wechat['id'] ?>"/>
                                    <input type="hidden" name="pids[<?php echo $_wechat['id'] ?>]"
                                           value="<?php echo $wechat['id'] ?>"/>
                                    <td><?php echo $_wechat['id'] ?></td>
                                    <td><i class="icon icon-arrow-right"></i> <?php echo $wechat['name'] ?></td>
                                    <td style="padding:0 0.1rem"><input type="text" class="form-control"
                                                                        name="names[<?php echo $_wechat['id'] ?>]"
                                                                        value="<?php echo $_wechat['name'] ?>"/></td>
                                    <td style="padding:0 0.1rem"><input type="text" class="form-control"
                                                                        name="keys[<?php echo $_wechat['id'] ?>]"
                                                                        value="<?php echo $_wechat['key'] ?>"/></td>
                                    <td style="padding:0 0.1rem"><input type="text" class="form-control"
                                                                        name="urls[<?php echo $_wechat['id'] ?>]"
                                                                        value="<?php echo $_wechat['url'] ?>"/></td>
                                    <td><?php echo $_wechat['type'] ?></td>
                                </tr>
                            <?php }
                        } ?>
                        <tbody>
                    </table>
                    <div class="clearfix mt-3"></div>

                    <p class="text-center">
                        <button type="submit" class="btn btn-primary" id="submit" data-loading-text="正在提交..."
                                style="width: 10rem;"><?php echo lang('confirm'); ?></button>
                        <button type="submit" class="btn btn-success" id="post" data-loading-text="正在提交..."
                                style="width: 10rem;"><?php echo lang('wechat_menu_push'); ?>
                        </button>
                        <a role="button" class="btn btn-secondary" style="width: 10rem;"
                           href="javascript:history.back();"><?php echo lang('back'); ?></a>
                    </p>

                </form>
            </div>
            <p class="m-a-1 small text-danger text-small">注：先保存菜单,然后推送 Save the menu first, and then push</p>
        </div>
    </div>
</div>

<?php include _include(ADMIN_PATH.'view/htm/footer.inc.htm');?>

<script>
    $('#wechat .icon').click(function () {
        var fid = $(this).attr("fid");
        if ( $(this).attr("class") == 'icon-plus' ) {
            $(this).attr("class", "icon-minus");
            $('.forum_son_' + fid).show();
        } else {
            $(this).attr("class", "icon-plus");
            $('.forum_son_' + fid).hide();
        }
    });

    var jform   = $("#form");
    var jsubmit = $("#submit");
    var jpost   = $("#post");
    var referer = '<?php echo http_referer();?>';
    jform.on('submit', function () {
        jform.reset();
        jsubmit.button('loading');
        var postdata = jform.serialize();
        $.xpost(jform.attr('action'), postdata, function (code, message) {
            if ( code == 0 ) {
                $.alert(message);
                jsubmit.text(message).delay(2000).button('reset').location(referer);
                return;
            } else {
                $.alert(message);
                jsubmit.button('reset');
            }
        });
        return false;
    });
    jpost.on('click', function () {
        jpost.button('loading');
        $.xpost(jform.attr('action'), { menu: 1 }, function (code, message) {
            if ( code == 0 ) {
                jpost.text(message);
                setTimeout(function () {
                    jpost.button('reset');
                }, 1000);
                return true;
            } else {
                $.alert(message);
                jpost.button('reset');
            }
        });
        return false;
    });
</script>